package cn.edu.tju.elm.controller;

import cn.edu.tju.elm.common.response.HttpResult;
import cn.edu.tju.elm.model.entity.DeliveryAddress;
import cn.edu.tju.elm.service.DeliveryAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "配送地址管理", description = "提供配送地址的增删改查功能，用户可以管理自己的收货地址")
public class DeliveryAddressController {
    private final DeliveryAddressService deliveryAddressService;

    public DeliveryAddressController(DeliveryAddressService deliveryAddressService) {
        this.deliveryAddressService = deliveryAddressService;
    }

    @PostMapping("/addresses")
    @Operation(
            summary = "添加配送地址",
            description = "为当前登录用户添加一个新的配送地址"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "地址添加成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "400", description = "请求参数无效")
    })
    public HttpResult<DeliveryAddress> addDeliveryAddress(
            @Parameter(description = "配送地址信息", required = true)
            @RequestBody DeliveryAddress deliveryAddress) {
        return HttpResult.ok(deliveryAddressService.addDeliveryAddress(deliveryAddress));
    }

    @GetMapping("/addresses")
    @Operation(
            summary = "获取用户配送地址列表",
            description = "获取当前登录用户的所有配送地址列表"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问")
    })
    public HttpResult<List<DeliveryAddress>> listDeliveryAddress(){
        return HttpResult.ok(deliveryAddressService.listDeliveryAddress());
    }

    @DeleteMapping("/addresses/{id}")
    @Operation(
            summary = "删除配送地址",
            description = "删除指定ID的配送地址，只能删除当前用户自己的地址"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "只能删除自己的地址"),
            @ApiResponse(responseCode = "404", description = "地址不存在")
    })
    public HttpResult<DeliveryAddress> deleteDeliveryAddress(
            @Parameter(description = "配送地址ID", required = true, example = "1")
            @PathVariable Long id){
        return HttpResult.ok(deliveryAddressService.deleteDeliveryAddress(id));
    }

    @PutMapping("/addresses/{id}")
    @Operation(
            summary = "更新配送地址",
            description = "全量更新指定ID的配送地址信息，只能更新当前用户自己的地址"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "只能更新自己的地址"),
            @ApiResponse(responseCode = "404", description = "地址不存在")
    })
    public HttpResult<DeliveryAddress> updateDeliveryAddress(
            @Parameter(description = "配送地址ID", required = true, example = "1")
            @PathVariable Long id,
            @Parameter(description = "更新后的配送地址信息", required = true)
            @RequestBody DeliveryAddress deliveryAddress){
        return HttpResult.ok(deliveryAddressService.updateDeliveryAddress(id, deliveryAddress));
    }
}